import java.util.Iterator;

/**
 * 带两个哨兵的 双向链表
 */
public class DoublyLikedListSentinel implements Iterable<Integer> {


    private static class Node {
        int value;   //节点存储的值
        Node prev;
        Node next;

        public Node(Node prev, int value, Node next) {
            this.value = value;
            this.prev = prev;
            this.next = next;
        }
    }

    //头哨兵
    private Node head;
    //尾哨兵
    private Node tail;

    public DoublyLikedListSentinel() {
        head = new Node(null, 001, null);
        tail = new Node(null, 999, null);
        head.next = tail;
        tail.prev = head;
    }

    public Node findNode(int index) {
        int i = -1;
        for (Node p = head; p != tail; p = p.next, i++) {
            if (index == i) {
                return p;
            }
        }
        return null;
    }

    private IllegalArgumentException illegalIndex(int index) {
        throw new IllegalArgumentException(String.format("index[%d]不合法%n", index));
    }

    /**
     * 插入某个位置的元素
     *
     * @param index
     * @param value
     */
    public void insert(int index, int value) {
        Node prevNode = findNode(index - 1);
        if (prevNode == null) {
            throw illegalIndex(index);
        }
        Node nextNode = prevNode.next;
        Node inserted = new Node(prevNode, value, nextNode);
        prevNode.next = inserted;
        nextNode.prev = inserted;
    }

    /**
     * 移除首个
     */
    public void removeFirst() {
        remove(0);
    }

    /**
     * 按index移除某个元素
     *
     * @param index
     */
    public void remove(int index) {
        Node removed = findNode(index);
        if (removed == null) {
            throw illegalIndex(index);
        }
        if (removed == tail) {
            throw illegalIndex(index);
        }
        Node nextNode = removed.next;
        Node prevNode = removed.prev;
        nextNode.prev = prevNode;
        prevNode.next = nextNode;
    }

    /**
     * 左插入
     */
    public void addFirst(int value) {
        Node nextNode = head.next;
        Node inserted = new Node(head, value, nextNode);
        head.next = inserted;
        nextNode.prev = inserted;
    }

    /**
     * 尾插入
     */
    public void addLast(int value) {
        Node prevNode = tail.prev;
        Node insertedNode = new Node(prevNode, value, tail);
        prevNode.next = insertedNode;
        tail.prev = insertedNode;
    }

    /**
     * 尾删
     */
    public void removeLast() {
        Node removed = tail.prev;
        if (removed == head) {
            throw illegalIndex(0);
        }
        Node prevNode = removed.prev;
        prevNode.next = tail;
        tail.prev = prevNode;
    }

    @Override
    public Iterator<Integer> iterator() {
        return new Iterator<Integer>() {
            Node p = head.next;

            @Override
            public boolean hasNext() {  //是否还有下一个，只要不要尾哨兵 都认为还有下一个
                return p != tail;
            }

            @Override
            public Integer next() {  //指针指向下一个节点，并且 返回当前节点的值
                int value = p.value;
                p = p.next;
                return value;
            }
        };
    }

    public static void main(String[] args) {
        DoublyLikedListSentinel doublyLikedListSentinel = new DoublyLikedListSentinel();
        doublyLikedListSentinel.addFirst(1);
        doublyLikedListSentinel.addLast(2);
        doublyLikedListSentinel.addLast(3);
        doublyLikedListSentinel.addLast(4);
        doublyLikedListSentinel.addFirst(0);
        doublyLikedListSentinel.forEach(System.out::println);
        doublyLikedListSentinel.insert(1,9);
        doublyLikedListSentinel.forEach(System.out::println);
        doublyLikedListSentinel.remove(1);
        doublyLikedListSentinel.forEach(System.out::println);
    }
}